package com.yazi.mxz.mobile.controller.user;

import javax.annotation.Resource;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Address;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.service.AddressService;
import com.yazi.mxz.mobile.jackson.filter.AddressFilter;
import com.yazi.mxz.mobile.jackson.filter.MemberFilter;

@Controller("user-addressController")
@RequestMapping("/user/address")
public class AddressController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(AddressController.class);

	@Resource
	AddressService addressService;

	@RequestMapping
	public String list() {
		log.debug("进入收货地址列表页面");
		return "v1.5/user/address_list";
	}
	
	@RequestMapping("/list/json")
	@ResponseBody
	public ObjectResult<Member> listJson() {
		log.debug("获取收货地址列表");
		ObjectResult<Member> jsonResult = new ObjectResult<Member>();
		jsonResult.setFilter(MemberFilter.getWithDefAddressAndAddresses());
		try {
			jsonResult.setResult(getMember());
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("显示收货地址异常:", e);
			jsonResult.setErrorMessage("加载失败，请联系管理员");
		}
		return jsonResult;
	}

	@RequestMapping("/addOrEdit")
	public String add() {
		log.debug("进入收货地址添加/编辑页面");
		return "v1.5/user/address_addOrEdit";
	}
	
	@RequestMapping("/one4Edit/json")
	@ResponseBody
	public ObjectResult<?> one4EditJson(Long addressId) {
		log.debug("获取收货地址");
		ObjectResult<Address> jsonResult = new ObjectResult<Address>();
		jsonResult.setFilter(AddressFilter.get4Edit());
		try {
			// 添加
			if (null == addressId) {
				jsonResult.setOk();
				return jsonResult;
			}
			// 编辑
			Address address = addressService.one(addressId);
			if (null == address || !address.getMember().equals(getMember())) {
				jsonResult.setFailMessage("非法操作");
				return jsonResult;
			}
			jsonResult.setResult(address);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取收货地址异常：", e);
			jsonResult.setErrorMessage("操作失败，请联系管理员。");
		}
		return jsonResult;
	}

	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public StatusResult addDoJson(Address address, Boolean isDefault) {
		StatusResult jsonResult = new StatusResult();
		if (null == address.getAddressId()) {
			log.debug("保存新增收货地址");
			try {
				// 强制把地址归属到当前用户下
				address.setMember(getMember());
				addressService.add(address, isDefault);
				jsonResult.setOkMessage("添加成功");
			} catch (Exception e) {
				log.error("收货地址添加异常：", e);
				jsonResult.setErrorMessage("添加失败，请联系管理员。");
			}
		} else {
			log.debug("保存修改收货地址");
			try {
				addressService.editByMember(getMember(), address, isDefault);
				jsonResult.setOkMessage("保存成功");
			} catch (MyException e) {
				jsonResult.setFailMessage(e.getMessage());
			} catch (Exception e) {
				log.error("保存收货地址异常:", e);
				jsonResult.setErrorMessage("收货地址修改失败，请联系管理员。");
			}
		}
		return jsonResult;
	}

	@RequestMapping("/delDo/json")
	@ResponseBody
	public ObjectResult<?> remove(Long addressId) {
		log.debug("删除收货地址");
		ObjectResult<Address> jsonResult = new ObjectResult<Address>();
		jsonResult.setFilter(AddressFilter.get());
		try {
			Address defAddress = addressService.removeByMember(getMember(), addressId);
			jsonResult.setResult(defAddress);
			jsonResult.setOkMessage("删除成功");
		} catch (Exception e) {
			log.error("删除收货地址异常：", e);
			jsonResult.setErrorMessage("操作失败，请联系管理员。");
		}
		return jsonResult;
	}
}
